package com.pioneer.cloudstorage.service.impl;

import com.pioneer.cloudstorage.utils.FileContentMapper;
import com.pioneer.cloudstorage.pojo.entity.FileContentIndex;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FileSearchService {

    @Autowired
    private FileContentMapper fileContentRepository;

    /**
     * 搜索文件内容
     */
    public List<FileContentIndex> searchFileContent(Long userId, String keyword, int page, int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            // 这里可以使用更复杂的 Elasticsearch 查询
            // 简化版本：在内容中搜索关键词
            return fileContentRepository.findByUserIdAndContentContaining(userId, keyword);
        } catch (Exception e) {
            log.error("搜索文件内容失败，用户ID: {}, 关键词: {}", userId, keyword, e);
            throw new RuntimeException("搜索失败");
        }
    }

    /**
     * 搜索文件名
     */
    public List<FileContentIndex> searchFileName(Long userId, String keyword, int page, int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            return fileContentRepository.findByUserIdAndFileNameContaining(userId, keyword);
        } catch (Exception e) {
            log.error("搜索文件名失败，用户ID: {}, 关键词: {}", userId, keyword, e);
            throw new RuntimeException("搜索失败");
        }
    }
}